<template>
  <div>
    <el-dialog :title="info.isAdd ? '规格添加' : '规格修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrsArr"
          :key="index"
        >
          <div class="inputAndBtn">
            <el-input v-model="item.val" autocomplete="off"></el-input>
            <el-button type="primary" v-if="index == 0" @click="attrsArr.push({ val: '' })">新增规格属性</el-button>
            <el-button type="danger" v-else @click="attrsArr.splice(index, 1)">删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqspecsadd, reqspecsinfo, reqspecsedit } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        specsname: "",
        attrs: "",
        status: 1
      },
      attrsArr: [
        {
          val: ""
        },
        {
          val: ""
        }
      ]
    };
  },
  computed: {
    ...mapGetters({
      arr: "specs/arr"
    }),
    showArr() {
      return this.arr.filter(item => item.id != this.user.id);
    }
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "specs/reqArr"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.specsname == "") {
          errorAlert("规格名称不能为空");
          return;
        }
        if (this.attrsArr.some(item => item.val === "")) {
          errorAlert("规格属性不能为空");
          return;
        }

        if (
          new Set(this.attrsArr.map(item => item.val)).size <
          this.attrsArr.length
        ) {
          errorAlert("规格属性不能重复");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        this.user.attrs = this.attrsArr.map(item => item.val).join(",");
        reqspecsadd(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1
      };
      this.attrsArr = [
        {
          val: ""
        },
        {
          val: ""
        }
      ];
    },
    getOne(id) {
      reqspecsinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list[0];
          this.attrsArr = this.user.attrs.map(item => ({ val: item }));
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        this.user.attrs = this.attrsArr.map(item => item.val).join(",");
        reqspecsedit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            // 如果修改的是本账号的信息，清空用户数据，并跳转至登录
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>